from module_admin.entity.vo.rule_vo import RulePageQueryModel,RuleModel
from module_admin.dao.rule_dao import RuleDao
from sqlalchemy.ext.asyncio import AsyncSession
from utils.common_util import CamelCaseUtil, export_list2excel
from module_admin.entity.vo.common_vo import CrudResponseModel


class RuleService:
    """
    业务数据监控规则
    """

    @classmethod
    async def get_rule_list_service(
        cls, query_db: AsyncSession, query_object: RulePageQueryModel, is_page: bool = False
    ):
        """
        规则监控信息service

        :param request: Request对象
        :param query_object: 查询参数对象
        :return: 规则监控列表信息
        """
        rule_list_result = await RuleDao.get_rule_monitor_list(query_db, query_object, is_page)

        return rule_list_result

    @classmethod
    async def rule_detail_services(cls, query_db: AsyncSession, rule_id: int):

        rule = await RuleDao.get_rule_detail_by_id(query_db, rule_id=rule_id)
        if rule:
            result = RuleModel(**CamelCaseUtil.transform_result(rule))
        else:
            result = RuleModel(**dict())

        return result


    @classmethod
    async def add_rule_monitor_service(cls, query_db: AsyncSession, rule_model: RuleModel):
        try:
            await RuleDao.add_rule_monitor_dao(query_db, rule_model)
            await query_db.commit()
            return CrudResponseModel(is_success=True, message="添加规则成功")
        except Exception as e:
            await query_db.rollback()
            raise e
    @classmethod
    async def edit_rule_monitor_service(cls, query_db: AsyncSession, rule_model: RuleModel):
        try:
            edit_rule = rule_model.model_dump(exclude_unset=True)
            await RuleDao.edit_rule_monitor_dao(query_db, edit_rule)
            await query_db.commit()
            return CrudResponseModel(is_success=True, message="编辑规则成功")
        except Exception as e:
            await query_db.rollback()
            raise e
    @classmethod
    async def delete_rule_monitor_service(cls, query_db: AsyncSession, rule_ids: str):
        try:
            rule_ids = rule_ids.split(',')
            for rule_id in rule_ids:
                await RuleDao.delete_rule_monitor_dao(query_db, rule_ids)
            await query_db.commit()
            return CrudResponseModel(is_success=True, message="删除规则成功")
        except Exception as e:
            await query_db.rollback()
            raise e

    @classmethod
    async def changeStatus_rule_monitor_service(cls, query_db: AsyncSession, rule_model: RuleModel):
        try:
            edit_rule = rule_model.model_dump(exclude_unset=True)
            await RuleDao.edit_rule_monitor_dao(query_db, edit_rule)
            await query_db.commit()
            return CrudResponseModel(is_success=True, message="修改规则状态成功")
        except Exception as e:
            await query_db.rollback()
            raise e
